﻿using DecorationSys.Models;
using Entity;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 账号相关
    /// </summary>
    public class AccountController : Controller
    {
        IUserInfoBll _userInfoBll;

        public AccountController(IUserInfoBll userInfoBll)
        {
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 登录页
        /// </summary>
        /// <returns></returns>
        public IActionResult LoginView()
        {
            return View();
        }


        /// <summary>
        /// 注册页
        /// </summary>
        /// <returns></returns>
        public IActionResult RegisterView()
        {
            return View();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult Register(string account, string password)
        {

            //参数验证
            if (string.IsNullOrEmpty(account))
            {
                return MyJsonResult.GetErrorResult("请输入账号");
            }
            if (string.IsNullOrEmpty(password))
            {
                return MyJsonResult.GetErrorResult("请输入密码");
            }

            string msg;
            bool isSuccess = _userInfoBll.CreateUserInfo(account, password, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult Login(string account, string password)
        {
            //参数验证
            if (string.IsNullOrEmpty(account))
            {
                return MyJsonResult.GetErrorResult("请输入账号");
            }
            if (string.IsNullOrEmpty(password))
            {
                return MyJsonResult.GetErrorResult("请输入密码");
            }

            if (account.Contains("-") || account.Contains(" "))
            {
                return MyJsonResult.GetErrorResult("账号格式有误");
            }

            string msg;
            UserInfo userInfo;
            bool isSuccess = _userInfoBll.Login(account, password, out userInfo, out msg);

            if (isSuccess)
            {
                //把用户的信息存进session中
                HttpContext.Session.SetString("UserName", userInfo.UserName);
                HttpContext.Session.SetString("UserId", userInfo.Id);

                return MyJsonResult.GetSuccessResult(new
                {
                    UserName = userInfo.UserName,
                    UserId = userInfo.Id
                });
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public MyJsonResult Logout()
        {

            //删除session里的用户信息
            HttpContext.Session.Remove("UserName");
            //拿session中的用户信息
            string userName = HttpContext.Session.GetString("UserName");
            if (string.IsNullOrEmpty(userName))
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("登出失败");
            }
        }


    }
}
